import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/models/home/sleep_model.dart';
import 'package:share_fit/app/models/home/date_type_enum.dart';
import 'package:share_fit/app/tools/date_tool.dart';
import 'package:share_fit/generated/locales.g.dart';

typedef SleepType = (int type, Color color);

class SleepState {
  final List<DateType> dateTypes = <DateType>[].obs;
  final Rx<DateType> selectedDateType = DateType.day.obs;

  SleepSummary sleepSummary = SleepSummary();

  /// 周、月需要的汇总数据
  List<SleepSummary> sleepSummaries = [];

  /// 日、周、月的睡眠数据
  List<SleepModel> dataList = [];

  /// 零星小睡数据
  List<SleepModel> napList = [];

  /// 柱状图数据
  List<SleepModel> barChartDataList = [];

  /// 右上角日历选择的日期
  DateTime selectedDate = DateTime.now();
  String dateRangeText = DateTool.getYMDWithDash(DateTime.now()).replaceAll('-', '/');

  /// 保存有睡眠记录的日期列表
  Map<DateTime, List<bool>> dateListOfRecord = {};

  List<SleepType> sleepTypes = [];

  /// 深睡
  SleepType deepSleep = (1, const Color.fromRGBO(110, 43, 182, 1));

  /// 浅睡
  SleepType lightSleep = (2, const Color.fromRGBO(152, 92, 247, 1));

  /// 清醒
  SleepType awake = (3, const Color.fromRGBO(255, 196, 67, 1));

  /// 快速眼动
  SleepType rem = (4, const Color.fromRGBO(40, 255, 253, 1));

  /// 零星小睡
  SleepType naps = (5, const Color.fromRGBO(253, 56, 197, 1));

  Color low = Colors.orange.withOpacity(0.6);
  Color normal = Colors.green;
  Color height = Colors.red;

  int dateTypeBarIndex = 0;

  SleepState() {
    ///Initialize variables
    dateTypes.addAll([DateType.day, DateType.week, DateType.month, DateType.year]);

    /// 睡眠类型集合
    sleepTypes.addAll([deepSleep, lightSleep, awake, rem, naps]);
  }

  /// 睡眠时长参考值计算:6-10小时为正常，小于6小时为偏低，大于10小时为偏高
  Text get totalRefValueLevelText {
    int total = sleepSumWithoutAwake / 60 ~/ validDataLength;
    if (total < 6) {
      return Text(LocaleKeys.sleep_ratio_low.ts, style: TextStyle(color: low, fontSize: 48.sp));
    } else if (total >= 6 && total <= 10) {
      return Text(LocaleKeys.sleep_ratio_normal.ts, style: TextStyle(color: normal, fontSize: 48.sp));
    } else {
      return Text(LocaleKeys.sleep_ratio_height.ts, style: TextStyle(color: height, fontSize: 48.sp));
    }
  }

  get dsRefValue {
    int total = sleepSumWithoutAwake;
    int ds = deepSleepSum ~/ validDataLength;
    return total <= 0.0 ? 0.0 : ds / total * 100;
  }

  /// 深睡比例参考值计算:20%-60%为正常，小于20%为偏低，大于60%为偏高。
  get dsRefValueLevelText {
    double percent = dsRefValue;
    if (percent < 20) {
      return Text(LocaleKeys.sleep_ratio_low.ts, style: TextStyle(color: low, fontSize: 48.sp));
    } else if (percent >= 20 && percent <= 60) {
      return Text(LocaleKeys.sleep_ratio_normal.ts, style: TextStyle(color: normal, fontSize: 48.sp));
    } else {
      return Text(LocaleKeys.sleep_ratio_height.ts, style: TextStyle(color: height, fontSize: 48.sp));
    }
  }

  get lsRefValue {
    int total = sleepSumWithoutAwake;
    int ds = lightSleepSum ~/ validDataLength;
    return total <= 0.0 ? 0.0 : ds / total * 100;
  }

  /// 浅睡比例参考值计算:浅睡比例:小于55%为正常，大于等于55%为偏高.
  get lsRefValueLevelText {
    double percent = lsRefValue;
    if (percent < 55) {
      return Text(LocaleKeys.sleep_ratio_normal.ts, style: TextStyle(color: normal, fontSize: 48.sp));
    } else {
      return Text(LocaleKeys.sleep_ratio_height.ts, style: TextStyle(color: height, fontSize: 48.sp));
    }
  }

  get remRefValue {
    int total = sleepSumWithoutAwake;
    int ds = remSleepSum ~/ validDataLength;
    return total <= 0.0 ? 0.0 : ds / total * 100;
  }

  /// 快速眼动比例参考值计算:快速眼动:10%-30%为正常，小于10%为偏低，大于30%为偏高
  get remRefValueLevelText {
    double percent = remRefValue;
    if (percent < 10) {
      return Text(LocaleKeys.sleep_ratio_low.ts, style: TextStyle(color: low, fontSize: 48.sp));
    } else if (percent >= 10 && percent <= 30) {
      return Text(LocaleKeys.sleep_ratio_normal.ts, style: TextStyle(color: normal, fontSize: 48.sp));
    } else {
      return Text(LocaleKeys.sleep_ratio_height.ts, style: TextStyle(color: height, fontSize: 48.sp));
    }
  }

  /// 清醒次数参考值计算:清醒:0-1次为正常，大于1次为偏高。
  get awakeRefValueLevelText {
    int times = awakeTimes ~/ validDataLength;
    if (times <= 1) {
      return Text(LocaleKeys.sleep_ratio_normal.ts, style: TextStyle(color: normal, fontSize: 48.sp));
    } else {
      return Text(LocaleKeys.sleep_ratio_height.ts, style: TextStyle(color: height, fontSize: 48.sp));
    }
  }

  /// 获取周、月有真实数据的天数
  get validDataLength => dataList.where((element) => (element.sleepTime ?? 0) > 0).map((e) => e.calendar).toSet().length;

  get totalSleepSumText => DateTool.second2HM(totalSleepSum * 60 ~/ validDataLength);

  get deepSleepSumText => DateTool.second2HM(deepSleepSum * 60 ~/ validDataLength);

  get lightSleepSumText => DateTool.second2HM(lightSleepSum * 60 ~/ validDataLength);

  get awakeSleepSumText => DateTool.second2HM(awakeSleepSum * 60 ~/ validDataLength);

  get remSleepSumText => DateTool.second2HM(remSleepSum * 60 ~/ validDataLength);

  get napsSleepSumText => DateTool.second2HM(napsSleepSum * 60 ~/ validDataLength);

  get deepSleepSum {
    Iterable<SleepModel> iterable = dataList.where((element) => element.sleepType == 1);
    return iterable.isEmpty ? 0 : iterable.map((e) => e.sleepTime).reduce((value, element) => value! + element!);
  }

  get lightSleepSum {
    Iterable<SleepModel> iterable = dataList.where((element) => element.sleepType == 2);
    return iterable.isEmpty ? 0 : iterable.map((e) => e.sleepTime).reduce((value, element) => value! + element!);
  }

  get awakeSleepSum {
    Iterable<SleepModel> iterable = dataList.where((element) => element.sleepType == 3);
    return iterable.isEmpty ? 0 : iterable.map((e) => e.sleepTime).reduce((value, element) => value! + element!);
  }

  get awakeTimes {
    Iterable<SleepModel> iterable = dataList.where((element) => element.sleepType == 3 && element.sleepTime! > 0);
    return iterable.length;
  }

  get remSleepSum {
    Iterable<SleepModel> iterable = dataList.where((element) => element.sleepType == 4);
    return iterable.isEmpty ? 0 : iterable.map((e) => e.sleepTime).reduce((value, element) => value! + element!);
  }

  get napsSleepSum {
    Iterable<SleepModel> iterable = dataList.where((element) => element.sleepType == 5);
    return iterable.isEmpty ? 0 : iterable.map((e) => e.sleepTime).reduce((value, element) => value! + element!);
  }

  /// 汇总总的睡眠时长
  /// 为了保持和手表一致，需要将类型为清醒的睡眠去除
  get totalSleepForSummary {
    return deepSleepSum + lightSleepSum + remSleepSum + napsSleepSum;
  }

  get sleepSumWithoutNapsAndAwakeText => DateTool.second2HM(sleepSumWithoutAwake * 60 ~/ validDataLength);

  /// 计算深睡、浅睡、快速眼动睡眠类型的总时长
  get sleepSumWithoutAwake {
    return deepSleepSum + lightSleepSum + remSleepSum + napsSleepSum;
  }

  bool get isPieChartDataIsNotEmpty {
    return sleepSumWithoutAwake == 0 || (deepSleepSum == 0 && lightSleepSum == 0 && remSleepSum == 0 && awakeSleepSum == 0);
  }

  /// 计算所有所有睡眠类型的总时长
  get totalSleepSum {
    return deepSleepSum + lightSleepSum + awakeSleepSum + remSleepSum + napsSleepSum;
  }

  get deepSleepRate => totalSleepSum == 0 ? '--%' : '${(deepSleepSum / totalSleepSum * 100).toStringAsFixed(2)}%';

  get lightSleepRate => totalSleepSum == 0 ? '--%' : '${(lightSleepSum / totalSleepSum * 100).toStringAsFixed(2)}%';

  get awakeSleepRate => totalSleepSum == 0 ? '--%' : '${(awakeSleepSum / totalSleepSum * 100).toStringAsFixed(2)}%';

  get remSleepRate => totalSleepSum == 0 ? '--%' : '${(remSleepSum / totalSleepSum * 100).toStringAsFixed(2)}%';

  get napsSleepRate => totalSleepSum == 0 ? '--%' : '${(napsSleepSum / totalSleepSum * 100).toStringAsFixed(2)}%';
}
